package api.testng;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import api.*;
import api.utils.ExcelToMapUtils;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.github.crab2died.ExcelUtils;
import com.github.crab2died.exceptions.Excel4JException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

@Slf4j
public class ApiTestNg {

    public static final String dir = System.getProperty("user.dir") + File.separator + "data" + File.separator;
    public static final String filePath = dir + "apitest3.xlsx";

    public static final List<TestResult> allTestResult = new ArrayList<>();

    //map<String,TestStatistics> 或者
    public static final List<TestStatistics> allTestStatistics = new ArrayList<>();

    public ExcelWriter excelWriter;


    @Test(dataProvider = "excel", threadPoolSize = 50)
    public void testCaseNg(String buniess, Map<String, Object> paramMap, TestStatistics testStatistics) {
        System.out.println(paramMap);
        List<TestResult> results = ApiTest2.testCase(buniess, paramMap, testStatistics);
        log.warn("testng--------------" + buniess + " " + paramMap + "  " + results);
        allTestResult.addAll(results); //包含了所有业务组的
        System.out.println(testStatistics);

        paramMap.clear();
        //统计处理 //重试
    }


    @DataProvider(name = "excel", parallel = true)
    public Iterator<Object[]> excelData() {
        List<Object[]> list = new ArrayList<>();
        List<String> allBuniess = ExcelToMapUtils.getAllBuniess(filePath);
        for (int i = 0; i < allBuniess.size(); i++) { //业务
            String buniess = allBuniess.get(i);
            TestStatistics testStatistics = new TestStatistics();
            testStatistics.setBuniess(buniess);
            List<Map<String, Object>> listMap = ExcelToMapUtils.importExcel(filePath, i + 1);
            if (!listMap.isEmpty()) {
                listMap.forEach(map -> list.add(new Object[]{buniess, map, testStatistics}));
               // statisticsMap.put(buniess, testStatistics);
                allTestStatistics.add(testStatistics);
            }
        }
        return list.iterator();
    }


    @BeforeClass
    public void before() {
        SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-dd(HH_mm)");
        String time = format0.format(System.currentTimeMillis());
        System.out.println(time);
        String resultPath = dir + "result_" + time + ".xlsx";
        excelWriter = EasyExcel.write(resultPath).build();
    }

    @AfterClass
    public void after() {
        //allTestResult 分类写
        List<String> allBuniess = ExcelToMapUtils.getAllBuniess(filePath);
        for (int i = 0; i < allBuniess.size(); i++) {
            String buniess = allBuniess.get(i);
            Collection<TestResult> testResults = allTestResult.stream().filter(test -> test.getBuniess().equals(buniess)).
                    collect(Collectors.toList());
            if (!testResults.isEmpty()) {
                WriteSheet writeSheet = EasyExcel.writerSheet(i, buniess).head(TestResultEasyExcel.class).build();
                excelWriter.write(testResults, writeSheet);

            }
        }
        for (TestStatistics testStatistics : allTestStatistics) {
              if (testStatistics.getError() > 0) {
                    List<TestCase> testCases = testStatistics.getRetry();
                    List<String> urlList = testCases.stream().map(TestCase::getUrl).
                            distinct().collect(Collectors.toList());
                    testStatistics.setErrorApis(urlList.toString());
                }
        }
        WriteSheet writeSheet = EasyExcel.writerSheet("统计").head(TestStatistics.class).build();
        excelWriter.write(allTestStatistics, writeSheet);

        ApiTest2.retry(allTestStatistics,excelWriter);
        excelWriter.finish();  //close
    }


}
